package cn.com.dashboard.model;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一结果返回码
 *
 * @param <T>
 */
@Data
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    private static final String SUCCESS_CODE = "0";
    private static final String SUCCESS_MESSAGE = "操作成功";

    private String code;
    private String message;
    private T data;

    public CommonResult(){
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MESSAGE;
    }

    public CommonResult(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> CommonResult<T> failed(String errorCode, String errorMsg) {
        return new CommonResult(errorCode, errorMsg, null);
    }

    public void setFailed(String errorCode, String errorMsg ){
        this.code = errorCode;
        this.message = errorMsg;
    }


}
